<?php

class Admin_Model_DbTable_Resources extends Zend_Db_Table_Abstract {

    protected $_name = 'resources';

    public function getModulesGroup($idgroup, $band) {
        $s = ($band == 1) ? " group by R.module" : "";
        require APPLICATION_PATH . '/../library/conexion.php';
        $sql = "SELECT R.idresource, R.module, R.name, R.view, R.controller
                FROM resources R, groups G, group_resources GR
                WHERE G.idgroup = '$idgroup'
                AND G.idgroup = GR.idgroup AND GR.visible = 1 
                AND R.idresource = GR.idresource ORDER BY R.module 
                $s";
        $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
        $stmt->execute();
        return $stmt->fetchAll();
    }

    public function getResources($ids) {
        foreach ($ids as $value) {
            $data[] = $value['idresource'];
        }
        if (isset($data)) {
            $ids = implode($data, ',');
            return $this->fetchAll('idresource IN(' . $ids . ')');
        } else
            return array();
    }

    public function getModules() {
        require APPLICATION_PATH . '/../library/conexion.php';
        $sql = 'select module from resources group by module';
        $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
        $stmt->execute();
        $row = $stmt->fetchAll();
        return $row;
    }

    public function getControllers($module) {
        require APPLICATION_PATH . '/../library/conexion.php';
        $module = mysql_real_escape_string($module);
        $sql = "select idresource, controller, name from resources where module = '$module' group by controller";
        $stmt = new Zend_Db_Statement_Mysqli($db, $sql);
        $stmt->execute();
        $row = $stmt->fetchAll();
        return $row;
    }

    public function getNames($module, $c) {
        return $this->fetchAll("module = '$module' and controller = '$c'");
    }

    public function getActions($module, $controller) {
        $row = $this->fetchAll("controller = '$controller' and module = '$module'", "view desc");
        return $row;
    }

}

?>
